<?php

/**
 * 日历显示类
 * Class Calendar
 */
class Calendar
{
    private $year;
    private $month;
    private $weeks = array('日', '一', '二', '三', '四', '五', '六');
    private $monthName = '';
    private $calendar_str = '';

    private $firstDay;
    private $starDay;
    private $days;
    private $dateComponents;
    private $dayOfWeek;


    public function __construct($options = array())
    {
        $this->year = date('Y');
        $this->month = date('m');

        $vars = get_class_vars(get_class($this));
        foreach ($options as $key => $value) {
            if (array_key_exists($key, $vars)) {
                $this->$key = $value;
            }
        }

        $this->calendar_str = '';
    }

    public function display()
    {
        $this->firstDay = mktime(0, 0, 0, $this->month, 1, $this->year);
        $this->starDay = date('w', $this->firstDay);
        $this->days = date('t', $this->firstDay);

        // 获取本月第一天
        $this->dateComponents = getdate($this->firstDay);

        // 获取月份的英文单词
        $this->monthName = $this->dateComponents['month'];
        //当前月最后一周的天数
        $this->dayOfWeek = $this->dateComponents['wday'];

        $this->calendar_str .= '<div class="cnt calendar_widget">';
        $this->showChangeDate();
        $this->calendar_str .= '<table class="table table-bordered table-striped calendar">';
        $this->showWeeks();
        $this->showDays();
        $this->calendar_str .= '</table></div>';

        return $this->calendar_str;
    }

    private function showWeeks()
    {
        $this->calendar_str .= '<thead><tr>';
        foreach ($this->weeks as $title) {
            $this->calendar_str .= '<th>' . $title . '</th>';
        }
        $this->calendar_str .= '</tr></thead>';
    }

    private function showDays()
    {
        $this->calendar_str .= '<tbody data-year="'.$this->dateComponents['year'].'" data-month="'.$this->dateComponents['mon'].'"><tr>';
        //前置空天数
        for ($i = 0; $i < $this->starDay; $i++) {
            $this->calendar_str .= '<td>&nbsp;</td>';
        }
        //当前月显示天数
        for ($j = 1; $j <= $this->days; $j++) {

            $i++;

            $this->calendar_str .= '<td '.($j == date('d') ? 'style="background:#c0c0c0"': '').'>' . $j . '</td>';

            if ($i % 7 == 0) {
                $this->calendar_str .= '</tr><tr>';
            }

        }
        //后置空天数
        if($this->dayOfWeek != 7){
            for ($last_i = 0; $last_i < $this->dayOfWeek; $last_i++) {
                $this->calendar_str .= '<td>&nbsp;</td>';
            }
        }

        $this->calendar_str .= '</tr></tbody>';
    }

    private function showChangeDate()
    {
        $this->calendar_str .= '<h6>';
        $this->calendar_str .= '<a class="previous-month pull-left" href="?' . $this->preMonthUrl() . '"><i class="icon-chevron-left"></i></a>';
        $this->calendar_str .= $this->monthName . '&nbsp;&nbsp;'. $this->dateComponents['mon'] . '月&nbsp;&nbsp;' . $this->year;
        $this->calendar_str .= '<a class="next-month pull-right" href="?' . $this->nextMonthUrl() . '"><i class="icon-chevron-right"></i></a>';
        $this->calendar_str .= '</h6>';
    }

    private function preMonthUrl()
    {
        if ($this->month == 1) {
            $this->month = 12;
            $this->year = ($this->year <= 1970) ? 1970 : $this->year - 1;
        } else {
            $this->month--;
        }

        return 'year=' . $this->year . '&month=' . $this->month;
    }

    private function nextMonthUrl()
    {
        if ($this->month == 12) {
            $this->month = 1;
            $this->year = ($this->year >= 2038) ? 2038 : $this->year + 1;
        } else {
            $this->month++;
        }
        return 'year=' . $this->year . '&month=' . $this->month;
    }
}